﻿using System;
namespace Shapes
{
    abstract class Shape
    {
        protected int num_of_sides;
        protected List<int> sides = new List<int>();
        private string? _name;
        public abstract void Show();
    }
    interface Ishape
    {
        double Area();
        double Perimeter();
    }
    interface isLegal
    {
        bool isLegal();
    }
    class Triangle : Shape, Ishape, isLegal
    {
        public Triangle(int a, int b, int c)
        {
            num_of_sides = 3;
            sides = new List<int> { a, b, c };
        }
        public double Area()
        {
            double s = Perimeter() / 2;
            return Math.Sqrt(s * (s - sides[0]) * (s - sides[1]) * (s - sides[2]));
        }
        public double Perimeter()
        {
            return sides[0] + sides[1] + sides[2];
        }
        public bool isLegal()
        {
            if (sides[0] + sides[1] > sides[2] && sides[0] + sides[2] > sides[1] && sides[1] + sides[2] > sides[0])
                return true;
            return false;
        }
        public  override void Show()
        {
            Console.WriteLine("Triangle:length: " + sides[0] + " " + sides[1] + " " + sides[2]);
        }
    }
    class Rectagle : Shape, Ishape, isLegal
    {
        public Rectagle(int a, int b)
        {
            num_of_sides = 4;
            sides = new List<int> { a, b, a, b };
        }
        public double Area()
        {
            return sides[0] * sides[1];
        }
        public double Perimeter()
        {
            return 2 * (sides[0] + sides[1]);
        }
        public bool isLegal()
        {
            if (sides[0] == sides[2] && sides[1] == sides[3])
                return true;
            return false;
        }
        public override void Show()
        {
            Console.WriteLine("Rectagle:length: " + sides[0] + " width:" + sides[1]);
        }
    }
    class Square : Rectagle
    {
        public Square(int a) : base(a, a) { }
        public  void show()
        {
            Console.WriteLine("Square: length: " + sides[0]);
        }
    }
    class Circle : Shape, Ishape, isLegal
    {
        public Circle(int r)
        {
            num_of_sides = 0;
            sides = new List<int> { r };
        }
        public double Area()
        {
            return Math.PI * sides[0] * sides[0];
        }
        public double Perimeter()
        {
            return 2 * Math.PI * sides[0];
        }
        public bool isLegal()
        {
            return true;
        }
        public  override void Show()
        {
            Console.WriteLine("Circle: radius: " + sides[0]);
        }
    }
    class ShapeFactory
    {
        public static Shape CreateShape(string name, params int[] sides)
        {
            if (name == "Triangle")
            {
                return new Triangle(sides[0], sides[1], sides[2]);
            }
            else if (name == "Rectagle")
            {
                return new Rectagle(sides[0], sides[1]);
            }
            else if (name == "Square")
            {
                return new Square(sides[0]);
            }
            else if (name == "Circle")
            {
                return new Circle(sides[0]);
            }
            return null;
        }
        public static Shape RandomShape()
        {           
            Random rand = new Random();
            int shape = rand.Next(0,3);
            if (shape == 0)
            {
                return new Triangle(rand.Next(1, 10), rand.Next(1, 10), rand.Next(1, 10));
            }
            else if (shape == 1)
            {
                return new Rectagle(rand.Next(1, 10), rand.Next(1, 10));
            }
            else if (shape == 2)
            {
                return new Square(rand.Next(1, 10));
            }
            else if (shape == 3)
            {
                return new Circle(rand.Next(1, 10));
            }
            return null;
        }
    }
    class MainClass
    {
        public static void Main(string[] args)
        {
            List<Shape> shapes = new List<Shape>();
            for(int i=0; i<10; i++)
            {
                shapes.Add(ShapeFactory.RandomShape());
                shapes[i].Show();
            }
            double SumArea = 0;
            for(int i=0; i<10; i++)
            {
                if(shapes[i] is Ishape && shapes[i] is isLegal)
                {
                    if(((isLegal)shapes[i]).isLegal())
                    {
                        SumArea += ((Ishape)shapes[i]).Area();
                    }
                }
            }
            Console.WriteLine("Sum of the area of the 10 shapes: " + SumArea);

        }
    }
}